package colin.project.sso.server.test.tools;

import cn.hutool.core.io.FileUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.ext.DebugInterceptor;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * @Author joker
 * @Title CodeGenerator
 * @Description
 * @Date 2019/5/17 9:52 PM
 * @Since
 * @Version 1.0.0
 */
public class CodeGenerator {

    private SQLManager sqlManager;

    private GroupTemplate gt;

    @Before
    public void initSqlManager() throws IOException {
        ConnectionSource source = ConnectionSourceHelper.getSimple("com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1:3306/lm_sso", "root", "");
        DBStyle mysql = new MySqlStyle();
// sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
// 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
// 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

        //========分界线========//
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        Configuration cfg = Configuration.defaultConfiguration();
        gt = new GroupTemplate(resourceLoader, cfg);

    }

    @Test
    public void generatePoJoCode() throws Exception {
        sqlManager.genPojoCode("sys_app", "colin.project.sso.server.business.entity");
        sqlManager.genPojoCode("sys_user", "colin.project.sso.server.business.entity");
        sqlManager.genPojoCode("sys_permission", "colin.project.sso.server.business.entity");
        sqlManager.genPojoCode("sys_role", "colin.project.sso.server.business.entity");
        sqlManager.genPojoCode("sys_re_user_role", "colin.project.sso.server.business.entity");
        sqlManager.genPojoCode("sys_re_role_permission", "colin.project.sso.server.business.entity");
    }

    @Test
    public void generateDaoTemplate() throws IOException {
        Template t1 = gt.getTemplate("template/dao.md");
        t1.binding("targetEntity", "SysReRolePermission");
        Writer writer1 = configWriter(GenKit.getJavaSRCPath(),"colin.project.sso.server.business.dao","SysReRolePermissionDao");
        t1.renderTo(writer1);
        Template t2 = gt.getTemplate("template/dao.md");
        t2.binding("targetEntity", "SysRole");
        Writer writer2 = configWriter(GenKit.getJavaSRCPath(),"colin.project.sso.server.business.dao","SysRoleDao");
        t2.renderTo(writer2);
        Template t3 = gt.getTemplate("template/dao.md");
        t3.binding("targetEntity", "SysReUserRole");
        Writer writer3 = configWriter(GenKit.getJavaSRCPath(),"colin.project.sso.server.business.dao","SysReUserRoleDao");
        t3.renderTo(writer3);
        Template t4 = gt.getTemplate("template/dao.md");
        t4.binding("targetEntity", "SysUser");
        Writer writer4 = configWriter(GenKit.getJavaSRCPath(),"colin.project.sso.server.business.dao","SysUserDao");
        t4.renderTo(writer4);
    }


    private Writer configWriter(String srcPath, String pkg, String targetClass) throws IOException {
        String file = srcPath + File.separator + pkg.replace('.', File.separatorChar);
        File f = new File(file);
        if (!f.exists()) {
            boolean succ = f.mkdirs();
            if (!succ) {
                throw new IOException("创建文件夹失败 " + f);
            }
        }
        File target = new File(file, targetClass + ".java");

        return new FileWriter(target);
    }
}
